<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <template> <!-- vue实例外创建 -->
          <el-tree #default="{data}" :data="list" default-expand-all>
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;font-size:14px">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
                <!-- <span>{{ data.children }}</span> -->
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id,false)">添加部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hAdd(data.id,true)">修改部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.aaa" @click.native="hdel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-tree>
        </template>
      </el-card>
    </div>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <dialogDepar v-if="dialogVisible" :form-change="formChange" :my-id="childrenId" :code-list="codeList" @hSuccess="hSuccess" />
    </el-dialog>

  </div>
</template>
<script>
import { DelDepartmentsList, GetDepartmentsList } from '@/api/departments'
import dialogDepar from './dialog.vue'
export default {
  components: {
    dialogDepar
  },
  data() {
    return {
      dialogVisible: false, // dialog开关信息
      formChange: false, // form表单是哪个信息
      // 依赖一份树形数据
      childrenId: '',
      list: [],
      codeList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 删除部门
    hdel(id) {
      DelDepartmentsList(id)
      this.getList()
    },
    async getList() {
      const res = await GetDepartmentsList()
      console.log(res)
      this.list = this.tranListToTreeData(res.data.depts)
      this.list.forEach((item) => { item.aaa = item.children.length })
      this.codeList = this.list.map(item => item.code)
      // console.log(arr)
      // console.log(this.list)
    },
    tranListToTreeData(list, pid = '') {
      // 先找到所有的根节点
      const tranList = list.filter(item => { return item.pid === pid })
      tranList.forEach(item => {
        // 传入id  list进行递归 在筛选出 他的父级 是一个数组
        item.children = this.tranListToTreeData(list, item.id)
      })
      return tranList
    },
    // 递归写法
    // 添加信息 打开dialog
    hAdd(id, change) {
      this.dialogVisible = true
      this.childrenId = id
      this.formChange = change
    },
    // diglog表单已提交
    hSuccess() {
      this.dialogVisible = false
      this.getList()
    }
  }
}
</script>
